package com.ohos.carbon.slice.componentList;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.model.ComponentRatingDataBean;
import com.ohos.carbon.provider.ComponentRatingProvider;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ElementScatter;

import java.util.ArrayList;
import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;

public class RatingAndTextSlice extends AbilitySlice {

    private Image back_button;
    private Text text_title, text;
    private ListContainer listContainer;
    private ArrayList<ComponentRatingDataBean> mList;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_rating_and_text);
        initData();
        initView();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Avatar,text,rating,subtext and date list item")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Avatar,text,rating,subtext and date list item")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Avatar,text,rating,subtext and date list item", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Avatar,text,rating,subtext and date list item", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Avatar,text,rating,subtext and date list item", 1);
            pf.flushSync();
        }


    }

    private void initData() {
        mList = new ArrayList<>();

        Element favour = ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_star_yellow_24dp);
        Element unFavour = ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_star_gray_24dp);

        mList.add(new ComponentRatingDataBean(ResourceTable.Media_randomdata_man0, null, "Header", "2021/08/11", "huansod nisbid jodndibq", favour, favour, favour, unFavour, unFavour));
        mList.add(new ComponentRatingDataBean(ResourceTable.Media_randomdata_man9, null, "Dibsw Bidb", "16:34,13 3月", "Hjis iqnind indnd jcnwik mnqdn koqjsp", favour, favour, unFavour, unFavour, unFavour));
        mList.add(new ComponentRatingDataBean(ResourceTable.Media_randomdata_man1, null, "nsibd Nodnb Asi", "18:12,11 3月", "Lnjsnjs njcnj hqb nkj nkqnb koq qks", favour, favour, unFavour, unFavour, unFavour));
        mList.add(new ComponentRatingDataBean(ResourceTable.Media_randomdata_man2, null, "Bhubs Fihs Soj", "09:38,10 3月", "Pkomq sams niqbnd mskw qikn nkqn", favour, favour, favour, favour, favour));
        mList.add(new ComponentRatingDataBean(ResourceTable.Media_randomdata_man3, null, "Knisn Sbns Wbs", "19:15,19 3月", "Kjins jibnd oqmx ojdn infiw josnd mkqms kqnkd kqns", favour, favour, favour, favour, favour));

        mList.add(new ComponentRatingDataBean(ResourceTable.Media_randomdata_background0, null, "Header", "2021/08/11", "Pjinqs idnuw nkqnd nknd nidqniq okqk avcb iqsn ossm mkanx mak mzkmz mkN", favour, favour, favour, unFavour, unFavour));
        mList.add(new ComponentRatingDataBean(ResourceTable.Media_randomdata_man9, null, "Bhsh Aisb", "07:19,10 3月", "Onnx nxmnck nmax mkm mlkM bhabxuh csgqycd vydqv bhxajb bajnxb hqvq  bnabx", favour, favour, favour, favour, favour));
        mList.add(new ComponentRatingDataBean(ResourceTable.Media_randomdata_man5, null, "Gnis Ans", "17:39,10 3月", "Cjhbx nbmbcj jabxj nxmnbx mkmn mzmbb bhjvsb qusb bja jbs", favour, favour, favour, favour, unFavour));
        mList.add(new ComponentRatingDataBean(ResourceTable.Media_randomdata_man6, null, "khi Hjis", "10:28,23 3月", "Zhjn knx knkqn kjmdkq smjks pkspks majka qds", unFavour, unFavour, unFavour, unFavour, unFavour));

        mList.add(new ComponentRatingDataBean(ResourceTable.Media_randomdata_man7, null, "Header", "2021/08/11", "huansod nisbid jodndibq", favour, unFavour, unFavour, unFavour, unFavour));
        mList.add(new ComponentRatingDataBean(ResourceTable.Media_randomdata_man8, null, "Vhusb Hjis", "23:19,11 3月", "Pjinqs idnuw nkqnd nknd nidqniq okqk avcb iqsn ossm mkanx mak mzkmz mkN", favour, unFavour, unFavour, unFavour, unFavour));
        mList.add(new ComponentRatingDataBean(ResourceTable.Media_randomdata_man9, null, "Lnjsn Jkoj Mjss", "11:22,13 3月", "Ijnd lmdkm jskcnc kandk kdmnk mamxcx", favour, favour, unFavour, unFavour, unFavour));
        mList.add(new ComponentRatingDataBean(ResourceTable.Media_randomdata_man5, null, "Lhisnh Hjs Knbs", "06:08,18 3月", "Wnxn mnkncl klmw mcklmc mc", favour, favour, favour, favour, favour));
        mList.add(new ComponentRatingDataBean(ResourceTable.Media_randomdata_man1, null, "Cgugs Hhi", "10:28,23 3月", "Phhd ubdiq jxj wjjd eqfdwq", favour, favour, unFavour, unFavour, unFavour));

        mList.add(new ComponentRatingDataBean(ResourceTable.Media_randomdata_background0, null, "Header", "17:22,11 8月", "huansod nisbid jodndibq", favour, favour, favour, unFavour, unFavour));
        mList.add(new ComponentRatingDataBean(ResourceTable.Media_randomdata_man2, null, "Khhsn Vgug Ljis", "11:17,3月", "Qjidh hfnhifn infcfc injkl akmdlmd jmodmsk", favour, favour, favour, favour, unFavour));
        mList.add(new ComponentRatingDataBean(ResourceTable.Media_randomdata_man3, null, "Sjihn Ojis", "17:22,11 3月", "Thjd ijf ojfow ijfk jsfhj jlkdwj ijrow", favour, favour, unFavour, unFavour, unFavour));


    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("Avatar,text,rating...");
//        text = (Text) findComponentById(ResourceTable.Id_text);
//        text.setText(ResourceTable.String_text);
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_listview);
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        listContainer.setItemProvider(new ComponentRatingProvider(mList, getContext()));
        back_button.setClickedListener(component -> onBackPressed());
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
